package com.anydata.android.weatherforecast.weather.shell;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.anydata.android.widget.weatherclock.ConstData;
import com.anydata.android.widget.weatherclock.WeatherForecastAppWidget;
import com.anydata.android.R;


public class WeeklyForecast extends Activity {
	
    private static int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    
    private int cityIndex;
    
    /**
	 * Lock used when maintaining queue of requested updates.
	 */
	private static Object sLock = new Object();
    
    /**
	 * Request updates for the given widgets. Will only queue them up, you are
	 * still responsible for starting a processing thread if needed, usually by
	 * starting the parent service.
	 */
	public static void requestUpdate(int id) {
		synchronized (sLock) {
			appWidgetId =  id;
		}
	}
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent launchIntent = getIntent();
        Bundle extras = launchIntent.getExtras();
        if (extras != null) {
            appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        } 
        
        setContentView(R.layout.weeklyforecast);
        ListView list = (ListView) findViewById(R.id.Forecast4Days);
        /*  
        //This is for dummy data
        ArrayList listItem = new ArrayList();
        for(int i=0;i<4;i++)  
        {  
	        HashMap map = new HashMap();  
	        map.put("ItemImage", R.drawable.app_icon);
	        map.put("ItemTitle", "Level "+i);  
	        map.put("ItemText", "Finished in 1 Min 54 Secs, 70 Moves! ");  
	        listItem.add(map);
        }*/
        
        //This is real data
        List listItem = WeatherForecastAppWidget.fiveDaysForecast;
        List dataList = new ArrayList();
        for(int i=0; i<listItem.size(); i++)  
        {  
	        Map map = (Map)listItem.get(i);
	        Map dataMap = new HashMap();
	        dataMap.put("ItemImage", map.get(WeatherForecastAppWidget.ICON));
	        dataMap.put("ItemTitle", map.get(WeatherForecastAppWidget.DAY));  
	        
	        if(i != 0)
	        	dataMap.put("ItemText", map.get(WeatherForecastAppWidget.LOW) + " ~ " + map.get(WeatherForecastAppWidget.HIGH));
	        else
//	        	dataMap.put("ItemText", map.get(WeatherForecastAppWidget.TEMPERATURE));
	        	dataMap.put("ItemText", map.get(WeatherForecastAppWidget.LOW) + " ~ " + map.get(WeatherForecastAppWidget.HIGH));
	        dataMap.put("weather_condition", map.get(WeatherForecastAppWidget.WEATHER_CONDITION));	
	        dataList.add(dataMap);
        }
        
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,
        		dataList,
        		R.layout.weeklyforecastitem,    
        		new String[] {"ItemImage", "ItemTitle", "ItemText", "weather_condition"},   
        		new int[] {R.id.ItemImage, R.id.ItemTitle, R.id.ItemText, R.id.weather_condition}  
        );  
       
        list.setAdapter(listItemAdapter); 
        
        final SharedPreferences config = getSharedPreferences(ConstData.PREF_FILE_NAME, 0);
        
        //citySpinner = (Spinner) findViewById(R.id.cityspinner);
//        int city_index = config.getInt(ConstData.PREFS_CITY_FIELD_Key, ConstData.PREFS_CITY_KEY_DEFAULT);
        /*SpinnerUtil.restoreCityPrefs(this, citySpinner, ConstData.city, city_index);
        ArrayAdapter<String> city_adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ConstData.city);
        citySpinner.setAdapter(city_adapter);
        citySpinner.setSelection(city_index);
        
        tempFmtSpinner = (Spinner) findViewById(R.id.temper_fmt_spinner);
        String temp_format = String.valueOf(config.getString(ConstData.PREFS_TEMPTUR_FMT_FIELD_Key, ConstData.PREFS_TEMPTUR_FMT_DEFAULT));
        ArrayAdapter<String> temp_adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ConstData.tempFmtCode);
        tempFmtSpinner.setAdapter(temp_adapter);
        int tmp_index = temp_adapter.getPosition(temp_format);
        tempFmtSpinner.setSelection(tmp_index);
        
        this.btnOk=(Button) findViewById(R.id.ButtonOk);
        this.btnOk.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			Spinner spr = (Spinner)findViewById(R.id.cityspinner);
                int select_city_index = spr.getSelectedItemPosition();

                Spinner spr_temp = (Spinner)findViewById(R.id.temper_fmt_spinner);
                String select_tem_fmt = (String)spr_temp.getSelectedItem();
                
                // store off the user setting for city
                SharedPreferences.Editor configEditor = config.edit();
                configEditor.putInt(ConstData.PREFS_CITY_FIELD_Key, select_city_index);
                configEditor.putString(ConstData.PREFS_TEMPTUR_FMT_FIELD_Key, select_tem_fmt);
                configEditor.commit();
    			
                if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {

                	int updateRateSeconds = 2;
                    // tell the app widget manager that we're now configured
                    Intent resultValue = new Intent();
                    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                    setResult(RESULT_OK, resultValue);

                    Intent widgetUpdate = new Intent();
                    widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                    widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { appWidgetId });
                    widgetUpdate.setClass(v.getContext(), WeatherForecastAppWidget.class);
                    
                    sendBroadcast(widgetUpdate);
                 }

                
    			finish();
    			
    		}
        });
        
        this.btncxl=(Button) findViewById(R.id.ButtonCxl);
        this.btncxl.setOnClickListener(new ButtonCxlListener());*/
     }
}